<template>
  <div class="favorites-list">
    <el-empty v-if="favorites.length === 0" description="暂无收藏内容" />
    <el-row v-else :gutter="20">
      <el-col v-for="item in favorites" :key="item.id" :xs="24" :sm="12" :md="8" :lg="6">
        <el-card class="favorite-item" :body-style="{ padding: '0px' }">
          <div class="item-image-container">
            <img :src="formatCardContent(item).image" class="item-image" />
            <div class="item-category">
              <el-tag>{{ formatCardContent(item).category }}</el-tag>
            </div>
          </div>
          <div class="item-content">
            <h3 class="item-title">{{ formatCardContent(item).title }}</h3>
            <p class="item-description">{{ formatCardContent(item).description }}</p>
            <div v-if="formatCardContent(item).authorInfo" class="item-author">
              {{ formatCardContent(item).authorInfo }}
            </div>
            <div class="item-footer">
              <span class="item-date">收藏于 {{ formatDate(item.addedAt) }}</span>
              <div class="item-actions">
                <el-button type="text" @click="viewDetail(item)">
                  <el-icon><View /></el-icon>
                  查看
                </el-button>
                <el-button type="text" @click="removeFavorite(item.id)">
                  <el-icon><Delete /></el-icon>
                  取消收藏
                </el-button>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { View, Delete } from '@element-plus/icons-vue';
import { useFavoritesStore } from '@/stores/favoritesStore';

const router = useRouter();
const favoritesStore = useFavoritesStore();

// 获取收藏列表
const favorites = computed(() => favoritesStore.getFavorites);

// 格式化日期
const formatDate = (dateString) => {
  const date = new Date(dateString);
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  });
};

// 查看详情
const viewDetail = (item) => {
  // 根据收藏项目类型跳转到不同页面
  if (item.type === 'story') {
    router.push(`/story/${item.id}`);
  } else {
    router.push(`/heritage/${item.id}`);
  }
};

// 取消收藏
const removeFavorite = async (itemId) => {
  try {
    await favoritesStore.removeFavorite(itemId);
    ElMessage.success('已取消收藏');
  } catch (error) {
    ElMessage.error(error.message || '操作失败');
  }
};

// 格式化卡片内容
const formatCardContent = (item) => {
  return {
    title: item.title,
    description: item.description,
    image: item.coverImage,
    category: item.type === 'story' ? '传承故事' : item.category,
    authorInfo: item.type === 'story' ? `作者：${item.authorName}` : null
  };
};
</script>

<style scoped>
.favorites-list {
  padding: 20px 0;
}

.favorite-item {
  margin-bottom: 20px;
  transition: all 0.3s;
}

.favorite-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.item-image-container {
  position: relative;
}

.item-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.item-category {
  position: absolute;
  top: 12px;
  right: 12px;
}

.item-content {
  padding: 14px;
}

.item-title {
  margin: 0;
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.item-description {
  margin: 8px 0;
  font-size: 14px;
  color: #606266;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
}

.item-author {
  margin: 8px 0;
  font-size: 13px;
  color: #909399;
}

.item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 12px;
  font-size: 12px;
}

.item-date {
  color: #909399;
}

.item-actions {
  display: flex;
  gap: 8px;
}

.el-button [class*='el-icon'] + span {
  margin-left: 4px;
}
</style> 